package com.qiku.dao.impl;

import com.qiku.dao.UserDao;
import com.qiku.pojo.User;
import com.qiku.utils.DruidUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.SQLException;
import java.util.List;

public class UserDaoImpl implements UserDao {

    private QueryRunner qr = new QueryRunner(DruidUtils.getDataSource());

    /**
     * 通过用户名和密码查询用户对象
     * @param username
     * @param password
     * @return
     */
    @Override
    public User userLogin(String username, String password) {
        try {
            //准备sql
            String sql = "select * from user where username = ? and password = ? and is_del = 0";
            //执行sql
            User userlogin = qr.query(sql, new BeanHandler<>(User.class), username, password);
            return userlogin;
        } catch (SQLException e) {
            e.printStackTrace();
            return null;
        }
    }

    //根据用户名查询用户
    @Override
    public User findUserByName(String username) {
        try {
            //准备sql
            String sql = "select * from user where username = ?";
            //执行sql
            User user = qr.query(sql, new BeanHandler<>(User.class), username);
            return user;
        } catch (SQLException e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 查询所有用户
     * @return
     */
    @Override
    public List<User> findAllUser() {
        try {
            //准备sql
            String sql = "select * from user";
            //执行sql
            List<User> userList = qr.query(sql, new BeanListHandler<>(User.class));
            return userList;
        } catch (SQLException e) {
            e.printStackTrace();
            return null;
        }

    }

    /**
     * 用户注册
     * @param user 用户对象
     * @return 影响数据的条目行数 一般为0 或者 1
     */
    @Override
    public int userRegister(User user) {
        try {
            //编写sql  (null,?,?,?,0,0,null)
            String sql = "INSERT INTO `user` VALUES (?,?,?,?,?,?,?)";
            //执行sql
            int i = qr.update(sql, null, user.getUsername(),
                    user.getPassword(), user.getRegister_date(), user.getIs_root(), 0, null);
            return i;
        } catch (SQLException e) {
            e.printStackTrace();
            return 0;
        }
    }

    @Override
    public User findUserById(int id) {
        try {
//            根据id查询用户信息
            String sql = "select * from user where id = ?";
            User user = qr.query(sql, new BeanHandler<>(User.class), id);
            return user;
        } catch (SQLException e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 管理员注销用户或者恢复用户
     * @param isDel 用户状态
     * @param id 用户id
     * @return
     */
    @Override
    public int destroyUser(int isDel,int id) {
        try {
            //准备sql
            String sql = "update user set is_del = ? where id = ?";
            //执行注销
            int i = qr.update(sql,isDel,id);
            //返回影响行数
            return i;
        } catch (SQLException e) {
            e.printStackTrace();
            return 0;
        }
    }
}
